<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');

class WST_Controller extends CI_Controller
{
    public $all_cats;
    public $region=array();
    public $all_provinder;
    public $all_district;
    public $province_id;
    public $userdata;
    function __construct ()
    {
        parent::__construct();
        $this->load->database();
        $this->province_id = get_cookie(COOKIE_PROVINCE_ID,true);

        if(empty($this->province_id) || $this->province_id==""){
            $cookie = array(
              'name'=>  COOKIE_PROVINCE_ID,
                'value'=>'1',
                'expire'=>time()+86500,
                'path'=>'/'
            );
            set_cookie($cookie);
        }

        $this->load->driver('cache', array('adapter' => 'memcached', 'backup' => 'file'));

        $this->userdata = $this->session->userdata('member');
        if(empty($this->province_id)){
            $this->session->set_userdata(COOKIE_PROVINCE_ID,100);
            $this->province_id = $this->session->userdata(COOKIE_PROVINCE_ID);
        }
        $this->load->model(array("category_model",'district_model','provinder_model'));
        $this->all_provinder = $this->provinder_model->getAllProvinder();
        $this->all_cats = $this->category_model->all_cats();
    }
    public function is_login(){
       if(!empty($this->userdata)){
           return true;
       }
       return false;
    }
    public function is_admin(){
        if(self::is_login()){
             if($this->userdata['role_id']==1){
                 return true;
             }
        }
        return false;
    }

}

/**
 * Back end Controller
 *
 */
class Admin extends WST_Controller {
    var $admin_url;
    public $getPageSize=10;

    function __construct()
    {
        parent::__construct();
        if(!$this->is_admin()){
            redirect();
        }
        $folder = $this->router->fetch_directory();
        $this->layout->setLayout('default',$folder);
        $this->load->library(array('form_validation','pagination'));
    }

    public function get_search_paging($url, $total, $page_number, $limit = 20){
        $this->load->library('paging');
        $config['base_url'] = $url;
        $config['total_rows'] = $total;
        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Prev';
        $config['per_page'] = $limit;
        $config['cur_page'] = $page_number;
        $config['uri_segment'] = 9999;
        $config['num_links'] = 5;
        $config['use_page_numbers'] = true;
        $config['last_link'] = "Cuối";
        $config['first_link'] = 'Đầu';
        $config['prefix'] = '';
        $config['anchor_class']= 'class="link-page"';
        $config['cur_tag_open'] = '<span class="current-pager">';
        $config['cur_tag_close'] = '</span>';

        $config['page_query_string'] = false;
        $config['query_string_segment'] = 'offset';
        $this->paging->initialize($config);
        return $this->paging->create_links();
    }
    public function get_paging($url, $total, $page_number, $limit = 20,$page_query_string=FALSE){
        $this->load->library('paging');
        $config['base_url'] = $url;
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        $config['cur_page'] = $page_number;
        $config['uri_segment'] = 9999;
        $config['num_links'] = 6;
        $config['use_page_numbers'] = true;
        $config['prefix'] = 'trang-';
        $config['anchor_class']= 'class="link-page"';
        $config['cur_tag_open'] = '<a class="current-pager" href="javascript:void(0);">';
        $config['cur_tag_close'] = '</a>';
        $config['first_link'] = 'Trang Dau';
        $config['last_link'] = 'Cuoi trang';
        $config['page_query_string'] = $page_query_string;
        //$config['page_format'] = '-trang-{page}';
        $this->paging->initialize($config);
        return $this->paging->create_links();
    }
    function get_url_params($page_key = "offset", $page_size_key = "page_size")
    {
        $items = array();
        foreach ($_GET as $key => $value) {
            if ($key != "paging" && $key != $page_key && $key != $page_size_key) {
                $items[] = $key . '=' . urlencode($value);
            }
        }
        if (count($items) > 0) {
            return "?paging=1&" . join("&", $items);
        }
        return '?paging=1';
    }

}

/**
 * Default Front-end Controller
 *
 */
class Front_Controller extends WST_Controller {

    public $all_cats;
    public $region=array();
    public $all_provinder;
    public $all_district;
    public  function __construct()
    {
        parent::__construct();
        $this->load->library(array('form_validation','pagination'));
        $this->layout->setLayout('default','default');
    }
}
class Member extends WST_Controller {


    public  function __construct()
    {
        parent::__construct();

        if(!$this->is_login()){
            redirect('dang-nhap');
        }
        $this->load->library(array('form_validation','pagination'));
        $folder = $this->router->fetch_directory();
        if(!empty($folder)){
            $this->layout->setLayout('default',$folder);
        }

    }
    public function get_paging($url, $total, $page_number, $limit = 20,$page_query_string=FALSE){
        $this->load->library('paging');
        $config['base_url'] = $url;
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        $config['cur_page'] = $page_number;
        $config['uri_segment'] = 9999;
        $config['num_links'] = 6;
        $config['use_page_numbers'] = true;
        $config['prefix'] = '';
        $config['suffix']= ".html";
        $config['anchor_class']= 'class="link-page"';
        $config['cur_tag_open'] = '<span class="current-pager">';
        $config['cur_tag_close'] = '</span>';
        $config['next_link']='Sau';
        $config['first_link'] = 'Trang đầu';
        $config['last_link'] = 'Trang cuối';
        $config['prev_link'] = 'Trước';
        $config['page_query_string'] = $page_query_string;
        $this->paging->initialize($config);
        return $this->paging->create_links();
    }

}
